package com.itboye.jigongbao.activity

import `in`.srain.cube.views.ptr.PtrFrameLayout
import `in`.srain.cube.views.ptr.PtrHandler2
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AbsListView
import android.widget.AdapterView
import android.widget.ImageView
import android.widget.RelativeLayout
import com.itboye.jigongbao.R
import com.itboye.jigongbao.adpapter.JiYiBiListAdapter
import com.itboye.jigongbao.app.MyApplication
import com.itboye.jigongbao.base.BaseActivity
import com.itboye.jigongbao.bean.JiYiBiBean
import com.itboye.jigongbao.retrofit.base.ResultEntity
import com.itboye.jigongbao.retrofit.presenter.UserPrestener
import com.itboye.jigongbao.utils.ByTimeUtils.timeFormat
import com.itboye.jigongbao.utils.Const
import com.itboye.jigongbao.utils.Const.Companion.pageSize
import com.itboye.jigongbao.utils.SPUtils
import com.itboye.jigongbao.utils.TitleBarUtils
import com.itboye.jigongbao.widget.BasePtr
import kotlinx.android.synthetic.main.activity_ji_yi_bi_list.*
import java.io.Serializable
import java.util.*
import kotlin.collections.ArrayList

class JiYiBiListActivity : BaseActivity(), Observer {

    var ar: ArrayList<JiYiBiBean.JiYiBiEntity>? = null
    var today = timeFormat("yyyyMMdd", System.currentTimeMillis())
    override fun update(p0: Observable?, p1: Any?) {
        ptr_jiyibi_list_framelayout.refreshComplete()
        var entity: ResultEntity = p1 as ResultEntity
        if (entity.code != 0) {

        } else {
            when (entity.eventType) {
                UserPrestener.By_JgbRecord_queryWorker_Success -> {
                    var jiyiBiBean = entity.data as JiYiBiBean
                    setDataChanged(jiyiBiBean.list)
                }
                UserPrestener.By_JgbRecord_queryWorker_Fail -> {

                }
                UserPrestener.By_JgbRecord_queryOverseer_Success -> {
                    var jiyiBiBean = entity.data as JiYiBiBean
                    setDataChanged(jiyiBiBean.list)
                }
                UserPrestener.By_JgbRecord_queryOverseer_Fail -> {

                }
                else -> {
                }
            }
        }
    }

    fun setDataChanged(list: ArrayList<JiYiBiBean.JiYiBiEntity>) {
        if (pageIndex == 1) {
            ar!!.clear()
        }
        ar!!.addAll(list)
        adapter!!.notifyDataSetChanged()
    }

    var iv_actionbar_left: ImageView? = null
    var lay_actionbar_right: RelativeLayout? = null
    var userPrestener: UserPrestener? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_ji_yi_bi_list
    }

    internal var isAtTop = true
    internal var isAtBottom = false
    override fun initData() {
        TitleBarUtils().initTitlebarStyle1(this, this!!.trans_action_bar_jiyibi_list!!, "记工列表", R.drawable.img_back, "", 0, "记一笔")
        if (intent.getStringExtra("date") != null) {
            today = intent.getStringExtra("date")
        }
        BasePtr.setPagedPtrStyle(ptr_jiyibi_list_framelayout)
        var headerView = View.inflate(this, R.layout.header_jiyibi_list, null)
        lv_jiyibilist.addHeaderView(headerView)
        setListAdapter()
        userPrestener = UserPrestener(this)
        lv_jiyibilist.setOnItemClickListener(object : AdapterView.OnItemClickListener {

            override fun onItemClick(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                var intent = Intent(this@JiYiBiListActivity, JiYiBiActivity::class.java)
                intent.putExtra("type", "update")
                intent.putExtra("id", ar!![p2 - 1].id.toString())
                intent.putExtra("item_type_key", ar!![p2 - 1].item_type_key)
                intent.putExtra("model", ar!![p2 - 1] as Serializable)
                startActivity(intent)
            }
        })
        ptr_jiyibi_list_framelayout.setPtrHandler(object : PtrHandler2 {
            override fun onRefreshBegin(frame: PtrFrameLayout?) {
                pageIndex = 1
                query()
            }

            override fun onLoadMoreBegin(frame: PtrFrameLayout?) {
                pageIndex++
                query()
            }

            override fun checkCanDoRefresh(frame: PtrFrameLayout?, content: View?, header: View?): Boolean {
                return isAtTop
            }

            override fun checkCanDoLoadMore(frame: PtrFrameLayout?, content: View?, footer: View?): Boolean {
                return isAtBottom
            }

        })

        lv_jiyibilist!!.setOnScrollListener(object : AbsListView.OnScrollListener {
            override fun onScroll(p0: AbsListView?, p1: Int, p2: Int, p3: Int) {
                if (p1 == 0) {
                    var firstVisibleItemView = lv_jiyibilist!!.getChildAt(0)
                    if (firstVisibleItemView != null && firstVisibleItemView.getTop() == 0) {
                        Log.d("ListView", "##### 滚动到顶部 #####")
                        isAtTop = true
                        isAtBottom = false
                    } else {
                        isAtTop = false
                    }
                } else if ((p1 + p2) == p3) {
                    var lastVisibleItemView = lv_jiyibilist!!.getChildAt(lv_jiyibilist!!.getChildCount() - 1);
                    if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == lv_jiyibilist!!.getHeight()) {
                        Log.d("ListView", "##### 滚动到底部 ######")
                        isAtBottom = true
                        isAtTop = false
                    } else {
                        isAtBottom = false
                    }
                }
            }

            override fun onScrollStateChanged(p0: AbsListView?, p1: Int) {
            }

        })
    }

    override fun onResume() {
        super.onResume()
        query()
    }

    var pageIndex = 1
    fun query() {
        var role = SPUtils.getSp(Const.ROLE) as String
        when (role) {
            Const.WORKER -> {
                userPrestener!!.By_JgbRecord_queryWorker(null, null, MyApplication.instance!!.loginInfo!!.getMobile(), today, pageIndex, pageSize, this)
            }
            Const.FORCEMAN -> {
                userPrestener!!.By_JgbRecord_queryOverseer(null, null, MyApplication.instance!!.loginInfo!!.getMobile(), today, pageIndex, pageSize, this)
            }
            else -> {
            }
        }
    }

    private var adapter: JiYiBiListAdapter? = null

    private fun setListAdapter() {
        if (ar == null) {
            ar = ArrayList()
        }
        adapter = JiYiBiListAdapter(this, ar!!, R.layout.item_jiyibilist)
        lv_jiyibilist.adapter = adapter
    }

    override fun onClick(p0: View?) {
        when (p0) {
            iv_actionbar_left -> {
                finish()
            }
            lay_actionbar_right -> {
                var intent = Intent(this@JiYiBiListActivity, JiYiBiActivity::class.java)
                intent.putExtra("type", "add")
                startActivity(intent)
            }
            else -> {
            }
        }
    }

}
